package com.hmeter.mongodb;

import java.net.UnknownHostException;
import java.util.Map;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class MongoDbCreateSampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5390999833963494979L;
	
	private static final String HOST = "HOST";
	private static final String PORT = "PORT";
	private static final String DBNAME = "DBNAME";
	private static final String COLLECTIONNAME = "COLLECTIONNAME";
	private static final String WRITEDOCUMENTINTOCOLLECTIONBOOL = "WRITEDOCUMENTINTOCOLLECTIONBOOL";
	private static final String WRITEJSONCONTENTINTOCOLLECTIONBOOL = "WRITEJSONCONTENTINTOCOLLECTIONBOOL";
	private static final String DELIMITER = "DELIMITER";
	private static final String KEYVALUEPAIRS = "KEYVALUEPAIRS";
	private static final String JSONCONTENT = "JSONCONTENT";

	@Override
	public SampleResult sample(Entry arg0) {
		SampleResult result = new SampleResult();
		
		StringBuilder requestData = new StringBuilder();
		requestData.append("Mongo DB host:port: " + this.getHost() +":"+ this.getPort() + "\n");
		requestData.append("Mongo DB name: " + this.getDbName() + "\n");
		requestData.append("Collection name: " + this.getCollectionName() + "\n");
		requestData.append("Key value pairs to write: " + this.getKeyValuePairs() + "\n");
		requestData.append("Delimiter: " + this.getDelimiter() + "\n");
		requestData.append("JSON content to write: " + this.getJsonContent() + "\n");
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData.toString());
		result.setDataType(SampleResult.TEXT);
		
		result.sampleStart();
				
		if (this.isWriteDocumentIntoCollectionBool())
		{	
			try 
			{
				MongoDbCreateSamplerUtility mongoCreate = new MongoDbCreateSamplerUtility(this.getHost(), Integer.parseInt(this.getPort()), this.getDbName(), this.getCollectionName());
				Map<String, Object> parsedInputContent = mongoCreate.getMongoDbUtility().parseInputContent(this.getKeyValuePairs(), this.getDelimiter());
				result.setResponseData(mongoCreate.writeDocumentIntoCollection(parsedInputContent).getBytes());
				result.setSuccessful(true);
			}
			catch (UnknownHostException e1)
			{
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
			catch (Exception e2)
			{
				result.setResponseData(e2.getMessage().getBytes());
				result.setSuccessful(false);			
			}
		}
		
		if (this.isWriteJsonContentIntoCollectionBool())
		{
			try
			{
				MongoDbCreateSamplerUtility mongoCreate = new MongoDbCreateSamplerUtility(this.getHost(), Integer.parseInt(this.getPort()), this.getDbName(), this.getCollectionName());
				result.setResponseData(mongoCreate.writeJSONContentIntoCollection(this.getJsonContent()).getBytes());
				result.setSuccessful(true);
			}
			catch (UnknownHostException e1)
			{
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
			catch (Exception e2)
			{
				result.setResponseData(e2.getMessage().getBytes());
				result.setSuccessful(false);			
			}
		}

		result.sampleEnd();
		return result;
	}
	
	public void setHost(String host)
	{
		setProperty(HOST, host);
	}
	
	public String getHost()
	{
		return getPropertyAsString(HOST);
	}
	
	public void setPort(String port)
	{
		setProperty(PORT, port);
	}
	
	public String getPort()
	{
		return getPropertyAsString(PORT);
	}
	
	public void setDbName(String dbName)
	{
		setProperty(DBNAME, dbName);
	}
	
	public String getDbName()
	{
		return getPropertyAsString(DBNAME);
	}
	
	public void setCollectionName(String collectionName)
	{
		setProperty(COLLECTIONNAME, collectionName);
	}
	
	public String getCollectionName()
	{
		return getPropertyAsString(COLLECTIONNAME);
	}
	
	public void setKeyValuePairs(String keyValuePairs)
	{
		setProperty(KEYVALUEPAIRS, keyValuePairs);
	}
	
	public String getKeyValuePairs()
	{
		return getPropertyAsString(KEYVALUEPAIRS);
	}
	
	public void setDelimiter(String delimiter)
	{
		setProperty(DELIMITER, delimiter);
	}
	
	public String getDelimiter()
	{
		return getPropertyAsString(DELIMITER);
	}
	
	public void setJsonContent(String jsonContent)
	{
		setProperty(JSONCONTENT, jsonContent);
	}
	
	public String getJsonContent()
	{
		return getPropertyAsString(JSONCONTENT);
	}
	
	public boolean isWriteDocumentIntoCollectionBool()
	{
		return getPropertyAsBoolean(WRITEDOCUMENTINTOCOLLECTIONBOOL);
	}
	
	public void setWriteDocumentIntoCollectionBool(boolean writeDocumentIntoCollection)
	{
		setProperty(WRITEDOCUMENTINTOCOLLECTIONBOOL, writeDocumentIntoCollection);
	}
	
	public boolean isWriteJsonContentIntoCollectionBool()
	{
		return getPropertyAsBoolean(WRITEJSONCONTENTINTOCOLLECTIONBOOL);
	}
	
	public void setWriteJsonContentIntoCollectionBool(boolean writeJsonContentIntoCollection)
	{
		setProperty(WRITEJSONCONTENTINTOCOLLECTIONBOOL, writeJsonContentIntoCollection);
	}

}
